﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class CsRoomObjectRelaBll
    {
        private static object o = new object();

        public DataTable GetRoomRelaObjectData()
        {
            ReflashData();
            SqlWhere where = new SqlWhere();
            where.Info = new CsRoomObjectRela() { };
            //where.SelectColumn = @" RoomObjectRelaIdentifier,RoomTypeIdentifier,CsValidateObjectIdentifier ";
            return CsRoomObjectRela.Select(where, GlobalConfig.ConnectionString);
        }

        private void ReflashData()
        {
            DataTable dt_Rela = GetEmptyTable();
            DataRow dr_Rela;

            lock (o)
            {
                DataTable dt = FactoryClass.CreateCsRoomObjectRela().GetRoomObjectRelaData();
                foreach (DataRow dr in dt.Rows)
                {
                    dr_Rela = dt_Rela.NewRow();
                    dr[2] = CreateIdentifier.GetIdentifier();
                    dr_Rela["RoomObjectRelaIdentifier"] = dr[2];

                    dr_Rela["RoomTypeIdentifier"] = FactoryClass.CreateCsRoomType().GetCsRoomTypeIdentifier(dr[0].ToString());
                    dr_Rela["CsValidateObjectIdentifier"] = FactoryClass.CreateCsValidateObject().GetValidateObjectIdentifier(dr[1].ToString());
                    dt_Rela.Rows.Add(dr_Rela);
                }
                FactoryClass.CreateCsRoomObjectRela().UpdateRoomObjectRelaData(dt);
            }
            CsRoomObjectRela.InsertDatatable(dt_Rela, GlobalConfig.ConnectionString);
        }

        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.Info = new CsRoomObjectRela() { RoomObjectRelaIdentifier = @"-1" };
            where.SelectColumn = @" RoomObjectRelaIdentifier,RoomTypeIdentifier,CsValidateObjectIdentifier ";
            return CsRoomObjectRela.Select(where, GlobalConfig.ConnectionString);
        }



    }
}
